
package scoutobahn.ui;

import java.util.Collection;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JList;
import javax.swing.LayoutStyle;

import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerDot;

import scoutobahn.util.Tuple;

/**
 *
 * @author Florian
 */
public class View extends javax.swing.JFrame {

    private javax.swing.JButton calculateRouteButton;
    private javax.swing.JComboBox<String> autocompletionComboBoxFromTown;
    private javax.swing.JComboBox<String> autocompletionComboBoxToTown;
    private javax.swing.JLabel fromLabel;
    private javax.swing.JLabel toLabel;
    private javax.swing.JPanel panel;
    private javax.swing.JScrollPane highwayListScrollPane;
    private JList<String> highwayList;
    private JMapViewer mapView;

    /**
     * Creates new View.
     */
    public View() {
        initComponents();
    }

    private void initComponents() {

        panel = new javax.swing.JPanel();
        highwayListScrollPane = new javax.swing.JScrollPane();
        highwayList = new JList<String>();
        autocompletionComboBoxFromTown = new javax.swing.JComboBox<String>();
        autocompletionComboBoxToTown = new javax.swing.JComboBox<String>();
        calculateRouteButton = new javax.swing.JButton();
        fromLabel = new javax.swing.JLabel();
        toLabel = new javax.swing.JLabel();
        mapView = new JMapViewer();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Scoutobahn");
        panel.setBackground(new java.awt.Color(204, 255, 255));

        GroupLayout jPanel1Layout = new GroupLayout(panel);
        panel.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        highwayListScrollPane.setViewportView(highwayList);

        getjComboBoxFromTown().setEditable(true);

        getjComboBoxToTown().setEditable(true);
        getjComboBoxToTown().setToolTipText("");

        getjButtonStart().setText("Bring mich hin!");

        fromLabel.setText("Von:");

        toLabel.setText("Nach:");

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(
                                GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(
                            GroupLayout.Alignment.LEADING, false)
                        .addComponent(highwayListScrollPane)
                        .addComponent(
                                getjComboBoxFromTown(),
                                0, 272, Short.MAX_VALUE))
                    .addComponent(fromLabel))
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(
                        GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(toLabel)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(panel,
                            GroupLayout.DEFAULT_SIZE,
                            GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(getjComboBoxToTown(),
                                GroupLayout.PREFERRED_SIZE,
                                272, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(
                                LayoutStyle.ComponentPlacement.RELATED,
                                204, Short.MAX_VALUE)
                        .addComponent(getjButtonStart())))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING,
                    layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(
                        GroupLayout.Alignment.BASELINE)
                    .addComponent(fromLabel)
                    .addComponent(toLabel))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(
                        GroupLayout.Alignment.BASELINE)
                    .addComponent(
                            getjComboBoxFromTown(),
                            GroupLayout.PREFERRED_SIZE,
                            GroupLayout.DEFAULT_SIZE,
                            GroupLayout.PREFERRED_SIZE)
                    .addComponent(getjComboBoxToTown(),
                            GroupLayout.PREFERRED_SIZE,
                            GroupLayout.DEFAULT_SIZE,
                            GroupLayout.PREFERRED_SIZE)
                    .addComponent(getjButtonStart()))
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(
                        GroupLayout.Alignment.TRAILING)
                    .addComponent(highwayListScrollPane,
                            GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE)
                    .addComponent(panel,
                            GroupLayout.DEFAULT_SIZE,
                            GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pack();
    }

    /**
     * Adds the given Points to the JMapViewer.
     * @param waypoints A Collection of points defined by a
     * Tuple<Latitude,Longitude>.
     */
    public void addMapPoints(
            final Collection<Tuple<Double, Double>> waypoints) {
//        if (jMapViewer == null) {
//        jMapViewer = new JMapViewer();
        mapView.setSize(panel.getSize());
        panel.add(mapView);
        mapView.setVisible(true);
//        }

        for (Tuple<Double, Double> waypoint : waypoints) {
            mapView.addMapMarker(new MapMarkerDot(waypoint.getItemOne()
                    , waypoint.getItemTwo()));
        }
        mapView.setDisplayToFitMapElements(true, true, true);

    }

    /**
     * Removes all current markers frm the Map.
     */
    public void removeMapPoints() {
        if (mapView.getMapMarkerList().size() > 0) {
            mapView.removeAllMapMarkers();
        }
    }

    /**
     * Gets the Combobox containing the Name of the Town to start from.
     * @return The Combobox containing the Name of the Town to start from.
     */
    public javax.swing.JComboBox<String> getjComboBoxFromTown() {
        return autocompletionComboBoxFromTown;
    }

    /**
     * Gets the Combobox containing the Name of the last Town of the Route.
     * @return The Combobox containing the Name of the last Town of the Route.
     */
    public javax.swing.JComboBox<String> getjComboBoxToTown() {
        return autocompletionComboBoxToTown;
    }

    /**
     * Gets the button that, on click, starts the Algorithm.
     * @return The button that, on click, starts the Algorithm.
     */
    public javax.swing.JButton getjButtonStart() {
        return calculateRouteButton;
    }
    /**
     * Adds the list of highways to the view.
     * @param highways The list highways names to display
     */
    public void addHighwayList(
            final Collection<String> highways) {
        DefaultListModel<String> model = new DefaultListModel<String>();
        for (String highwayRef : highways) {
            model.addElement(highwayRef);
        }
        highwayList.setModel(model);
        highwayList.repaint();
    }

}


